package com.zyx.nettypro.netty.sticky.solver;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * @author Yaxi.Zhang
 * @since 2021/9/24 10:41
 */
public class MyMessageDecoder extends ReplayingDecoder<MessageProtocol> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("调用MyMessageDecoder的decode方法......");
        // 获取输入的ByteBuf流转化为 MessageProtocol 输出
        int length = in.readInt();
        byte[] content = new byte[length];
        in.readBytes(content);

        // 封装成 MessageProtocol 对象, 放入 out, 传递下一个handler业务处理
        MessageProtocol msg = new MessageProtocol();
        msg.setLength(length);
        msg.setContent(content);

        out.add(msg);
    }
}
